package com.luoxc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.luoxc.anno.MyNotLog;
import com.luoxc.mapper.EmpMapper;
import com.luoxc.pojo.Emp;
import com.luoxc.pojo.PageBean;
import com.luoxc.service.EmpService;
import com.luoxc.utils.JwtHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate start, LocalDate end) {

        PageHelper.startPage(page,pageSize);
        List<Emp> list = empMapper.list(name,gender,start,end);
        Page<Emp> p = (Page<Emp>) list;

        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public void deletes(List<Integer> ids) {
        empMapper.delete(ids);
    }
/*
* 新增数据
* */
    @Override
    public void add(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.add(emp);
    }
    /*
    * 根据id查询
    * */
    @Override
    public Emp queryId(Integer id) {
        Emp emp = empMapper.queryId(id);
        return emp;
    }
    /*
    * 根据id修改员工信息
    * */
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }
    @Transactional // 事务管理
    @Override
    public Map<String, Object> login(Emp emp) {
        Emp res = empMapper.getUsernameAndPassword(emp);
        if(res == null){
            return null;
        }
        // 获取token
        String jwt = JwtHandle.getJwt(res);
        Map<String,Object> map = new HashMap<>();
        map.put("userinfo",res);
        map.put("token",jwt);
        return map;
    }
}
